package com.hqhsc.module.front.activity.service;

import com.hqhsc.core.cache.CacheKey;
import com.hqhsc.module.common.service.CommonService;
import com.hqhsc.module.common.service.ManageMemcacdedClient;
import com.hqhsc.module.front.activity.dao.SignupDao;
import com.hqhsc.module.init.service.InitService;
import com.hqhsc.module.manage.activity.domain.Activity;
import com.hqhsc.module.manage.channel.domain.Channel;
import com.hqhsc.utils.DateTimeUtil;
import com.hqhsc.utils.DateUtil;
import com.hqhsc.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.util.*;

/**
 *
 * @author jiangyin
 */
@Service
public class SignupService {

	static Logger logger = LoggerFactory.getLogger(SignupService.class);

	@Autowired
	private ManageMemcacdedClient memcachedClient;
	@Autowired
	private SignupDao signupDao;
    @Autowired
    private CommonService commonService;
    @Autowired
    private InitService initService;
    @Autowired
    private Properties appProps;

    public Activity getActivityById(HttpServletRequest request , long activity_id , long channel_id) {
        Activity activity = this.commonService.getSysActivityById(activity_id);
        if(activity.getStatus() == 1) {//活动有效状态下，更新PV、UV
            try{
                //判断活动是否结束
                boolean is_activity_end = true;
                Date end_date = DateUtil.string2Date(activity.getEnd_time() , "yyyy-MM-dd");
                int seconds = DateTimeUtil.nDaysBetweenTwoDate(new Date() , end_date);
                if(seconds > 0) {
                    is_activity_end = false;
                }
                if(!is_activity_end) {//活动没结束
                    //判断是否需要更新UV
                    String mac_address;
//                    try{
//                        mac_address = Utils.getLocalMacAddress(InetAddress.getLocalHost());
//                    } catch (Exception e) {
                        mac_address = Utils.getIpAddr(request);
//                    }

                    String activity_channel_uv_object_key = CacheKey.getActivityChannelUVObjectKey(activity_id, channel_id, mac_address);
                    Object o = this.memcachedClient.get(activity_channel_uv_object_key);
                    boolean is_update_uv = false;
                    if(o == null) {//缓存没值，说明该ip，当天没有uv,需要更新uv值
                        is_update_uv = true;
                    }

                    //更新活动PV、UV、点击量
                    this.signupDao.updateActivityPvUv(activity_id , true , is_update_uv);

                    //更新渠道PV、UV、点击量
                    this.signupDao.updateChannelPvUv(channel_id , true , is_update_uv);

                    //更新活动、渠道对应表中PV、UV、点击量
                    this.signupDao.updateActivityChannelPvUv(activity_id , channel_id , true , is_update_uv);

                    //UV，0点失效
                    //计算当前时间，距离0点的秒数，作为缓存的失效时间
                    Calendar cur_date = Calendar.getInstance();
                    Calendar tom_date = new GregorianCalendar(cur_date.get(Calendar.YEAR), cur_date.get(Calendar.MONTH), cur_date.get(Calendar.DATE) + 1, 0, 0, 0);
                    int invalid_time = (int)(tom_date.getTimeInMillis() - cur_date.getTimeInMillis()) / 1000;
                    this.memcachedClient.set(activity_channel_uv_object_key , invalid_time , 1);
                }
            } catch (Exception e) {
                logger.error(e.getMessage() , e);
            }
        }
         //修改开始、结束时间
        String begin_time = activity.getBegin_time();
        String end_time = activity.getEnd_time();

        try{
            activity.setBegin_time(DateUtil.getDate(DateUtil.string2Date(begin_time , "yyyy-MM-dd HH:mm:ss") , "yyyy年MM月dd日 HH:mm"));
            activity.setEnd_time(DateUtil.getDate(DateUtil.string2Date(end_time , "yyyy-MM-dd HH:mm:ss") , "yyyy年MM月dd日 HH:mm"));
        } catch (Exception e) {}

        return activity;
    }

    public int signupActivityUser(long activity_id , long channel_id , String user_name , String sex ,
                                  String dn , String id_card , String address , String budget ,
                                  String wedding_date , String marry_require , String other_require) {
        long has_signup = this.signupDao.getActivityUserByDn(activity_id , dn);
        if(has_signup > 0) {
            return -1;
        }

        Activity activity = this.commonService.getSysActivityById(activity_id);
        Channel channel = this.commonService.getSysChannelById(channel_id);

        int result = this.signupDao.signupActivityUser(activity_id , channel_id , user_name , sex ,
                dn , id_card , address , budget , wedding_date , marry_require , other_require , activity.getName() , channel.getName());
        if(result > 0) {
            this.signupDao.updateActivitySignupCount(activity_id);
            this.signupDao.updateChannelSignupCount(channel_id);
            this.signupDao.updateActivityChannelSignupCount(activity_id , channel_id);

            String key = CacheKey.getActivityKey(activity_id);
            activity.setSignup_count(activity.getSignup_count()+1);
            this.memcachedClient.set(key , 30*24*60*60 , activity);
        }
        return result;
    }

}
